package com.soar.recruitment.module.web.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.soar.recruitment.R;
import com.soar.recruitment.common.ui.fragment.SOARBaseFragment;
import com.soar.recruitment.module.web.bll.SOARWebController;
import com.soar.recruitment.module.web.bll.interfaces.OnWebLoadListener;
import com.soar.recruitment.module.web.bll.interfaces.OnWebTitleListener;

import butterknife.BindView;

public class WebFragment extends SOARBaseFragment {

    private final static String KEY_URL = "url";
    private String mUrl;

    public static WebFragment newInstance(String url) {
        Bundle args = new Bundle();
        WebFragment fragment = new WebFragment();
        args.putString(KEY_URL, url);
        fragment.setArguments(args);
        return fragment;
    }

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.menu_icon)
    ImageView menu_icon;
    @BindView(R.id.menu_title)
    TextView menu_title;
    @BindView(R.id.root_base_webview)
    LinearLayout root_base_webview;

    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    private SOARWebController mKFQWebController;

    @Override
    public int initLayout() {
        return R.layout.fragment_web;
    }

    @Override
    public void initVariable() {
        Bundle bundle = getArguments();
        if(bundle != null) {
            mUrl = bundle.getString(KEY_URL, "");
        }
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        toolbar.setTitle("");
        initToolbarNav(toolbar);
        mKFQWebController = new SOARWebController(mActivity, mFragmentManager, root_base_webview, new OnWebTitleListener() {
            @Override
            public void onReceivedTitle(String title) {
                if(toolbar != null) {
                    toolbar.setTitle(title);
                }
            }

            @Override
            public void progressChanged(int newProgres) {
                if (progressBar != null) {
                    progressBar.setProgress(newProgres);

                    if(newProgres == 100) {
                        progressBar.setVisibility(View.GONE);
                    } else {
                        progressBar.setVisibility(View.VISIBLE);
                    }
                }
            }
        }, new OnWebLoadListener() {
            @Override
            public void showContent() {
                if (mLoadFrameLayout != null) {
                    mLoadFrameLayout.showContentView();
                }
            }

            @Override
            public void showError() {
                if (mLoadFrameLayout != null) {
                    mLoadFrameLayout.showErrorView();
                }
            }
        });
    }

    private void initToolbarNav(Toolbar toolbar) {
        toolbar.setNavigationIcon(R.mipmap.app_back_material_white);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mKFQWebController.isCanGoBack()) {
                    mKFQWebController.onGoBack();
                } else {
                    _mActivity.onBackPressed();
                }
            }
        });
    }

    @Override
    public void initData() {
        mKFQWebController.onLoadUrl(mUrl);
    }

}
